package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.dao.VenueReportDrawingMapper;
import com.eastfair.home.dto.VenueReportDrawingCategoryDTO;
import com.eastfair.home.dto.VenueReportDrawingDTO;
import com.eastfair.home.dto.VenueReportDrawingHistoryDTO;
import com.eastfair.home.entity.VenueReportDrawing;
import com.eastfair.home.enumeration.VenueReportReportDrawingStatusEnum;
import com.eastfair.home.exceptioncode.HomeExceptionCode;
import com.eastfair.home.service.*;
import com.eastfair.home.vo.VenueReportDrawingVO;
import com.eastfair.venueservice.vo.DrawingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 一键报馆图纸
 * </p>
 *
 * @author dq
 * @date 2022-10-31
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class VenueReportDrawingServiceImpl extends SuperServiceImpl<VenueReportDrawingMapper, VenueReportDrawing> implements VenueReportDrawingService {

    @Resource
    private HomeDrawingService homeDrawingService;

    @Resource
    private VenueReportService venueReportService;

    @Resource
    private VenueReportDrawingHistoryService venueReportDrawingHistoryService;

    @Resource
    private VenueManageVenueDrawingCategoryAuditService venueManageVenueDrawingCategoryAuditService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<VenueReportDrawing> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(VenueReportDrawing model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<VenueReportDrawingVO> listVenueReportDrawingVO(VenueReportDrawingDTO query) {
        log.info("listVenueReportDrawingVO - 查询图纸列表VO, venueReportDrawingDTO={}", query);
        List<VenueReportDrawing> list = listVenueReportDrawing(query);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list
                .stream()
                .map(venueReportDrawing -> BeanUtil.toBean(venueReportDrawing, VenueReportDrawingVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<VenueReportDrawing> listVenueReportDrawing(VenueReportDrawingDTO query) {
        log.info("listVenueReportDrawing - 查询图纸列表, venueReportDrawingDTO={}", query);
        QueryWrapper<VenueReportDrawing> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(VenueReportDrawing::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getVenueReportId() != null, VenueReportDrawing::getVenueReportId, query.getVenueReportId())
                .eq(query.getExhibitionManageId() != null, VenueReportDrawing::getExhibitionManageId, query.getExhibitionManageId())
                .eq(query.getDrawingId() != null, VenueReportDrawing::getDrawingId, query.getDrawingId())
                .eq(StrUtil.isNotBlank(query.getSpaceCode()), VenueReportDrawing::getSpaceCode, query.getSpaceCode())
        ;
        return list(queryWrapper);
    }

    @Override
    public VenueReportReportDrawingStatusEnum getReportDrawingStatus(Long exhibitionManageId, String spaceCode, Long venueReportId) {
        // 查询展会下该展厅上报的图纸
        VenueReportDrawingDTO param = new VenueReportDrawingDTO();
        param.setVenueReportId(venueReportId);
        param.setExhibitionManageId(exhibitionManageId);
        param.setSpaceCode(spaceCode);
        List<VenueReportDrawing> venueReportDrawingList = listVenueReportDrawing(param);
        if (venueReportDrawingList == null || venueReportDrawingList.isEmpty()) {
            return VenueReportReportDrawingStatusEnum.NOT_REPORT;
        }
        // 已报馆图纸类型的数量
        long venueReportDrawingCount = venueReportDrawingList
                .stream()
                .map(VenueReportDrawing::getDrawingId)
                .distinct()
                .count();
        // 综合服务设置的图纸类型的数量
        List<DrawingVO> drawingVOList = homeDrawingService.listVOByExhibitionId(exhibitionManageId, spaceCode);
        int drawingCount = drawingVOList.size();

        if (venueReportDrawingCount < drawingCount) {
            return VenueReportReportDrawingStatusEnum.PARTIAL_REPORT;
        } else {
            return VenueReportReportDrawingStatusEnum.HAS_REPORT;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public VenueReportDrawing saveVenueReportDrawing(VenueReportDrawingDTO venueReportDrawingDTO) {
        VenueReportDrawing venueReportDrawing = BeanUtil.toBean(venueReportDrawingDTO, VenueReportDrawing.class);
        save(venueReportDrawing);
        return venueReportDrawing;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<VenueReportDrawingDTO> list) {
        log.info("saveBatch - 批量提交图纸, list={}", list);
        list.forEach(this::saveVenueReportDrawing);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitBatch(VenueReportDrawingCategoryDTO param) {
        log.info("submitVenueReportDrawing - 提交报馆图纸, venueReportDrawingCategoryDTO={}", param);
        List<VenueReportDrawingDTO> list = param.getVenueReportDrawingList();
        if (list == null || list.isEmpty()) {
            throw BizException.wrap(HomeExceptionCode.REPORT_VENUE_DRAWING_NOT_EXIST);
        }
        // 查询展会展厅此类型的图纸
        VenueReportDrawingDTO query = new VenueReportDrawingDTO();
        query.setExhibitionManageId(param.getExhibitionManageId());
        query.setDrawingId(param.getDrawingId());
        query.setSpaceCode(param.getSpaceCode());
        query.setVenueReportId(param.getVenueReportId());
        List<VenueReportDrawing> existVenueReportDrawingList = listVenueReportDrawing(query);
        // 删除该展会展厅此类型的图纸
        removeByIdsOfLogic(existVenueReportDrawingList);
        // 批量保存展厅图纸
        saveBatch(list);
        // 更新图纸上报状态
        venueReportService.updateReportDrawingStatus(param.getVenueReportId());
        // 保存图纸提交历史
        VenueReportDrawingHistoryDTO historyDTO = BeanUtil.toBean(param, VenueReportDrawingHistoryDTO.class);
        venueReportDrawingHistoryService.saveVenueReportDrawingHistory(historyDTO);
        // 提交场馆图纸审核
        return venueManageVenueDrawingCategoryAuditService.submitByVenueReportDrawingCategory(param);
    }
}
